<template>
  <div class="me">
    <div class="top">
      <img class="bj" src="@/assets/img/me_bj.png" alt="">
      <div class="info">
        <van-uploader :after-read="afterRead">
          <img class="avatar" :src="userInfo.avatar || '@/assets/img/Avatar.png'" alt="">{{userInfo.nickname}}
        </van-uploader>
      </div>
    </div>
    <div class="box">
        <van-cell @click="$router.push('/me/birthday?v=' + userInfo.birthday)" title="出生日期" is-link :value="userInfo.birthday" />
        <van-cell @click="$router.push('/me/gender?v=' + userInfo.gender)" title="性别" is-link :value="userInfo.gender === 1 ? '男': '女'" />
        <van-cell @click="$router.push('/me/weight?v=' + userInfo.weight)" title="体重（kg）" is-link :value="userInfo.weight" />
        <van-cell @click="$router.push('/me/height?v=' + userInfo.height)" title="身高（cm）" is-link :value="userInfo.height" />
        <van-cell title="体重指数（BMI）" :value="BMI"  label="*说明：体重千克数除以身高米数的平方得出的数字（正常值：20-25）"/>
        <van-cell @click="$router.push('/phone?mobile=' + userInfo.mobile)" title="手机号" is-link :value="userInfo.mobile" />
        <van-cell @click="$router.push('/password')" title="密码" is-link value="" />
    </div>
    <button class="btn" @click="logout">退出登录</button>
  </div>
</template>
<script>
import { logout } from '@/api/login'
import { upload, editAvatar } from '@/api/common'
export default {
  name: 'Mine_',
  data () {
    return {

    }
  },
  computed: {
    BMI () {
      return (this.userInfo.weight / Math.pow(parseInt(this.userInfo.height) / 100, 2)).toFixed(2)
    },
    userInfo () {
      return this.$store.getters.userInfo
    }
  },
  methods: {
    async afterRead (e) {
      const fd = new FormData()
      // fd.append('name', 'file')
      fd.append('file', e.file)
      const { code, msg, data } = await upload(fd)
      if (code !== 1) return this.$toast.fail(msg)
      this.fullurl = data.fullurl
      this.uploadAvatar()
    },
    // 发送修改图片接口
    async uploadAvatar () {
      const { code, msg } = await editAvatar({ avatr_url: this.fullurl })
      if (code !== 1) return this.$toast.fail(msg)
      this.$toast.success('修改头像成功')
      // TODO 接口更新用户信息 || 本地修改用户信息-头像
      const _userInfo = this.userInfo
      _userInfo.avatar = this.fullurl
      this.$store.commit('SET_USER_INFO', _userInfo)
    },
    // 退出登录
    async logout () {
      const { code, msg } = await logout({ token: this.userInfo.token })
      if (code !== 1) return this.$toast.fail(msg)
      this.$toast.success(msg)
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      this.$router.push('/userType')
    }
  }
}
</script>
<style scoped lang='less'>
.me{
  width: 100%;
  // height: 100%;
  background-color: #FAFBFB;
  .top{
    position: relative;
    width: 375px;
    height: 216px;
    background: linear-gradient(156deg, #3EB3FF 0%, #436EFF 100%);
    opacity: 1;
    overflow: hidden;
    .bj{
      position: absolute;
      right: 0;
      bottom: 0;
    }
    .info{
      width: 100%;
      height: 74px;
      padding: 0 22px;
      box-sizing: border-box;
      margin-top: 75px;
      .avatar{
        width: 74px;
        height: 74px;
        border-radius: 50%;
        margin-right: 20px;
        vertical-align: middle;
      }
    }
  }
  .box{
    width: 92%;
    background-color: #fff;
    padding: 10px 15px;
    box-sizing: border-box;
    position: absolute;
    top: 186px;
    left: 4%;
  }
  /deep/.van-cell__title{
    font-size: 15px;
    font-weight: 600;
  }
  /deep/.van-cell__label{
    width: 210%;
    color: #ED4C4D;
  }
    .btn{
    width: 80%;
    height: 48px;
    margin: 340px 10% 60px;
    border: none;
    background: linear-gradient(180deg, #3EB3FF 0%, #436EFF 100%);
    box-shadow: 0px 8px 20px rgba(29, 90, 255, 0.32);
    border-radius: 37px;
    font-size: 16px;
    font-family: PingFang SC;
    font-weight: 600;
    line-height: 48px;
    color: #FFFFFF
  }
}
</style>
